import service from '@/api/service'
import type { InvoiceVo } from '@/type/Invoice'
import type { Page, PageQuery } from '@/type/common/list-types'
import type { InvoiceStatisticsDayOfMonth } from '@/type/invoice-types'

const mapping = 'invoice'

/** 条件分页查询发票列表 */
export const invoiceListApi = (pageQuery: PageQuery) =>
  service.post<Page<InvoiceVo[]>>(`${mapping}/page`, pageQuery)

/** 根据ID查询发票详细信息 */
export const invoiceInfoApi = (id: string) => service.get<InvoiceVo>(`${mapping}/${id}`)

/** 导入CSV文件 */
export const importInvoiceApi = (type: string, file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return service.post(`${mapping}/import`, formData, {
    params: { type },
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}

/** 查询汇总余额 */
export const invoiceStatisticsBalanceApi = (year?: string) => {
  if (year === undefined) return service.get<number>(`${mapping}/statistics/balance`)
  else return service.get<number>(`${mapping}/statistics/balance?year=${year}`)
}

/** 天-月 */
export const invoiceStatisticsDayOfMonthApi = (month: string) =>
  service.get<InvoiceStatisticsDayOfMonth>(`${mapping}/statistics/dayOfMonth/${month}`)

/** 月-年 */
export const invoiceStatisticsMonthOfYearApi = (year: string) =>
  service.get<InvoiceStatisticsDayOfMonth>(`${mapping}/statistics/monthOfYear/${year}`)